﻿using NewLife.Configuration;
using NewLife.Xml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace XsClient.Common
{
    /// <summary>
    /// XsSourceConfig
    /// 创建人：zhangyang
    /// 创建时间：2020/6/5 10:46:44
    /// </summary>
    public class XsSource
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 集成路径
        /// </summary>
        public string BaseUrl { get; set; }
        /// <summary>
        /// 原始地址，通过访问可以获取最新地址
        /// </summary>
        public string SrcUrl { get; set; }
        public string Encoding { get; set; } = "utf8";
        public override string ToString()
        {
            return Name.ToString();
        }
    }
    /// <summary>
    /// 小说源
    /// </summary>
    public class XsConfig //: Config<XsConfig>
    {
        private const string xmlName = "Xs.config";
        public List<XsSource> Sources { get; set; } = new List<XsSource>();
        /// <summary>
        /// 默认
        /// </summary>
        public int DefaultID { get; set; }
        public XsSource FindById(int id)
        {
            try
            {
                if (Sources != null)
                {
                    var src = Sources.SingleOrDefault(p => p.ID == id);
                    return src;
                }

            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
            return null;
        }
        private static XsConfig localConfig = null;
        /// <summary>
        /// 当前对象
        /// </summary>
        public static XsConfig Current
        {
            get
            {
                try
                {
                    if (localConfig == null)
                    {
                        string fullPath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config"), xmlName);
                        localConfig = XmlHelper.ToXmlFileEntity<XsConfig>(fullPath, Encoding.UTF8);
                    }
                }
                catch (Exception ex)
                {
                    NewLife.Log.XTrace.WriteException(ex);
                }
                if (localConfig == null)
                    localConfig = new XsConfig();

                return localConfig;
            }
        }
        public void Save()
        {

            try
            {
                
                string fullPath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config"), xmlName);
                XmlHelper.ToXmlFile(this, fullPath, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }
    }

    public class BookList //: Config<BookList>
    {
        private const string xmlName = "BookList.config";

        private List<BookInfo> books = null;
        public List<BookInfo> Books
        {
            get
            {
                if (books == null)
                    books = new List<BookInfo>();
                return books;
            }
            set { books = value; }
        }


        private static BookList localBookList = null;
        /// <summary>
        /// 当前对象
        /// </summary>
        public static BookList Current
        {
            get
            {
                try
                {
                    if (localBookList == null)
                    {
                        string fullPath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config"), xmlName);
                        localBookList = XmlHelper.ToXmlFileEntity<BookList>(fullPath, Encoding.UTF8);
                    }
                }
                catch (Exception ex)
                {
                    NewLife.Log.XTrace.WriteException(ex);
                }
                return localBookList;
            }
        }
        public void Save()
        {
            try
            {
                string fullPath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config"), xmlName);
                XmlHelper.ToXmlFile(this, fullPath, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }
    }
    public class BookInfo
    {
        /// <summary>
        /// 数据源ID
        /// </summary>
        [XmlAttribute]
        public int SourceID { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        [XmlAttribute]
        public string Name { get; set; }
        /// <summary>
        /// 作者
        /// </summary>
        [XmlAttribute]
        public string Author { get; set; }
        /// <summary>
        /// 集成路径
        /// </summary>
        [XmlAttribute]
        public string BaseUrl { get; set; }
        /// <summary>
        /// 当前章节
        /// </summary>
        [XmlAttribute]
        public string NowChapterID { get; set; }
        /// <summary>
        /// 当前章节名称
        /// </summary>
        [XmlAttribute]
        public string NowChapterName { get; set; }


        /// <summary>
        /// 阅读章节
        /// </summary>
        [XmlAttribute]
        public string ReadChaperName { get; set; }

        private List<BookSourceItem> _Sources = null;
        /// <summary>
        /// 其他源
        /// </summary>
        public List<BookSourceItem> Sources
        {
            get { if (_Sources == null) _Sources = new List<BookSourceItem>(); return _Sources; }
            set
            {
                _Sources = value;
            }
        }

    }
    public class BookSourceItem
    {
        /// <summary>
        /// 数据源ID
        /// </summary>
        [XmlAttribute]
        public int SourceID { get; set; }

        /// <summary>
        /// 集成路径
        /// </summary>
        [XmlAttribute]
        public string BaseUrl { get; set; }
    }

    /// <summary>
    /// 本地书架
    /// </summary>
    //[XmlConfigFile("LocalBookList.config",30000)]
    public class LocalBookList //: Config<LocalBookList>
    {
        private const string xmlName = "LocalBookList.config";

        private static LocalBookList localBookList = null;

        public static LocalBookList Current
        {
            get
            {
                try
                {
                    if (localBookList == null)
                    {
                        string fullPath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config"), xmlName);
                        localBookList = XmlHelper.ToXmlFileEntity<LocalBookList>(fullPath, Encoding.UTF8);
                    }
                }
                catch (Exception ex)
                {
                    NewLife.Log.XTrace.WriteException(ex);
                }
                return localBookList;
            }
        }

        private List<LocalBook> books = null;
        public List<LocalBook> Books
        {
            get
            {
                if (books == null)
                    books = new List<LocalBook>();
                return books;
            }
            set { books = value; }
        }

        public void Save()
        {
            try
            {
                string fullPath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config"), xmlName);
                XmlHelper.ToXmlFile(this, fullPath, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }
    }
    /// <summary>
    /// 本地小说
    /// </summary>
    public class LocalBook
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath { get; set; }
        /// <summary>
        /// 阅读章节
        /// </summary>
        public string ReadChaperName { get; set; }
    }
}
